package com.JobFind.DAO.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.JobFind.DAO.IDeliverDAO;
import com.JobFind.model.TbDeliver;

@Component
public class DeliverDAO implements IDeliverDAO{
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Integer adDeliver(TbDeliver deliverBean)
			throws Exception {
		Integer result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			result = (Integer) session.save(deliverBean);
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbDeliver> getResumeList(Integer companyId) throws Exception {
		List<TbDeliver> result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbDeliver where tbRecruitment.recruitmentId in (select recruitmentId from TbRecruitment where tbCompany.companyId=?) and tbUser.tbCompany.companyId=1 and deliverStatus=0";
			Query query = session.createQuery(hql);
			query.setInteger(0, companyId);
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<TbDeliver> getPassRersonList(Integer companyId) throws Exception {
		List<TbDeliver> result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbDeliver where tbRecruitment.recruitmentId in (select recruitmentId from TbRecruitment where tbCompany.companyId=?) and deliverStatus=1";
			Query query = session.createQuery(hql);
			query.setInteger(0, companyId);
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}
	
	public Integer upDeliver(Integer deliverId, Integer deliverStatus)
			throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "update TbDeliver set deliverStatus = ? where deliverId = ?";
			Query query = session.createQuery(hql);
			query.setInteger(0, deliverStatus);
			query.setInteger(1, deliverId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbDeliver> getResumeResult(Integer userId) throws Exception {
		List<TbDeliver> result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbDeliver where tbUser.userId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, userId);
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}

}
